<?php
/**
 * Created by PhpStorm.
 * User: zsm
 * Date: 2018/5/11
 * Time: 13:07
 */

namespace Library;
class Yin_Exception extends \Exception
{
    /**
     * @author zsm
     */
    public function printStack()
    {
        if (true === Config('debug')) {
            echo parent::getTraceAsString();
        } else {
            $this->_toLogFile(parent::getTraceAsString());
            $this->_outputErrorPage();
        }

    }

    public function __toString()
    {
        if (true !== Config('debug')) {
            $this->_toLogFile(parent::getTraceAsString());
            $this->_outputErrorPage();
            exit();
        }
        return parent::__toString();

    }

    protected function _toLogFile($str)
    {
        file_put_contents(APP_PATH . 'Runtime/Logs/' . date('Ymd') . '.txt', date('Y-m-d H:i:s').'   '.$str.PHP_EOL, FILE_APPEND);
    }

    protected function _outputErrorPage()
    {
        header("content-type:text/html");
        echo file_get_contents(APP_PATH . '/error.html');
    }
}